﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;

namespace Polymod.Silverlight.TestHarness
{
    public class Order
    {
        string _customerName;
        string _orderNumber;
        List<OrderItem> _items = new List<OrderItem>();

        /// <summary>
        /// Gets or Sets CustomerName
        /// </summary>
        public string CustomerName
        {
            get { return _customerName; }
            set { _customerName = value; }
        }

        /// <summary>
        /// Gets or Sets OrderNumber
        /// </summary>
        public string OrderNumber
        {
            get { return _orderNumber; }
            set { _orderNumber = value; }
        }

        /// <summary>
        /// Gets Items
        /// </summary>
        public List<OrderItem> Items
        {
            get { return _items; }
        }

        public Order(string customerName, string orderNumber)
        {
            _customerName = customerName;
            _orderNumber = orderNumber;
        }
    }

    public class OrderItem
    {
        string _itemCode;
        decimal _price;

        /// <summary>
        /// Gets or Sets ItemCode
        /// </summary>
        public string ItemCode
        {
            get { return _itemCode; }
            set { _itemCode = value; }
        }

        /// <summary>
        /// Gets or Sets Price
        /// </summary>
        public decimal Price
        {
            get { return _price; }
            set { _price = value; }
        }

        public OrderItem(string itemCode, decimal price)
        {
            _itemCode = itemCode;
            _price = price;
        }

        public override string ToString()
        {
            return string.Format("{0} @ {1:c}", ItemCode, Price);
        }
    }

    public class ModelFactory
    {
        private readonly static Polymod.ModelFactory _modelFactory;

        /// <summary>
        /// Gets ModelFactory
        /// </summary>
        public static Polymod.ModelFactory Factory
        {
            get { return _modelFactory; }
        }
    
        static ModelFactory()
        {
            var templateBuilder = new Polymod.TemplateBuilders.FormulaModelTemplateBuilder();
            templateBuilder.For<Order>().CreateFormula("Total", m => m.Items.Sum(i => i.Price));
            //templateBuilder.For<Order>().WhenChanged("OrderNumber", m => MessageBox.Show("Total is now " + m.Get("Total")));


            _modelFactory = new Polymod.ModelFactory(templateBuilder);
        }
    }
}
